﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Nasa8x.Mvc.Controls
{
    public static class RadioButtonListHelper
    {


        public static string RadioButtonList(this HtmlHelper helper, string name, IEnumerable<string> items)
        {

            var selectList = new SelectList(items);

            return helper.RadioButtonList(name, selectList);

        }

        public static string RadioButtonList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {

            TagBuilder tableTag = new TagBuilder("table");

            tableTag.AddCssClass("radio-main");



            var trTag = new TagBuilder("tr");

            foreach (var item in items)
            {

                var tdTag = new TagBuilder("td");

                var rbValue = item.Value ?? item.Text;

                var rbName = name + "_" + rbValue;

                var radioTag = helper.RadioButton(rbName, rbValue, item.Selected, new { name = name });



                var labelTag = new TagBuilder("label");

                labelTag.MergeAttribute("for", rbName);

                labelTag.MergeAttribute("id", rbName + "_label");

                labelTag.InnerHtml = rbValue;



                tdTag.InnerHtml = radioTag.ToString() + labelTag;



                trTag.InnerHtml += tdTag.ToString();

            }

            tableTag.InnerHtml = trTag.ToString();



            return tableTag.ToString();

        }
    }
}
